package com.zyzd.custom.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.zyzd.custom.R;

/**
 * Created by xinggang_yin on 2018/4/27 0027.
 * description ${TODO}
 * <p>
 * Changed by somebody on 2018/4/27 0027
 * description ${TODO}
 */

public class ProtractorView extends View {


    private Paint mPaint;

    private int mReserved = 2;//用于矫正线的厚度，避免两边的线部分未能展示
    private float mInnerCircleRatio = 0.3f; //内圆与量角器外圆的比例
    private float mLongRulingRadio = 0.05f;//短刻度与量角器外圆半径的比例
    private float mBottomSpanRadio = 0.05f;//底部白版与量角器外圆半径的比例


    private float mWidth;
    private float mHeight;

    private float mLongRulingSize;//长刻度
    private float mOutCircleRadius;//外圆半径
    private float mBottomSpanHeight;//底部白板高度
    private int mPanelColor = R.color.color_cccc;//量角器面板颜色
    private float mInnerRadius;


    public ProtractorView(Context context) {
        this(context, null);
    }

    public ProtractorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProtractorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);

        float expectRadioOfWH = 2f / (1f + mBottomSpanRadio);// 期望的宽高比
        float realRadioOfWH = (measureWidth - mReserved) * 1f / (measureHeight - mReserved);

        //如果实际宽高比大于期望宽高比，则以高为标准进行绘制,反之以宽为标准
        if (realRadioOfWH > expectRadioOfWH) {
            setMeasuredDimension((int) ((measureHeight - mReserved) / (1 + mBottomSpanRadio) * 2 + 0.5f), measureHeight);
        } else {
            setMeasuredDimension(measureWidth, (int) ((measureWidth - mReserved) / 2f * (1 + mBottomSpanRadio) + mReserved + 0.5f));
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mWidth = w;
        mHeight = h;

        mOutCircleRadius = (mWidth - mReserved) / 2f;
        mBottomSpanHeight = mOutCircleRadius * mBottomSpanRadio;
        mLongRulingSize = mOutCircleRadius * mLongRulingRadio;
        mInnerRadius = mOutCircleRadius * mInnerCircleRatio;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.translate(mWidth / 2f, mHeight - mBottomSpanHeight - mReserved / 2f);

        //绘制整体框架和背景
        drawProtractorFrame(canvas);
        //绘制刻度和文本
        drawRulingAndText(canvas);

    }

    private void drawRulingAndText(Canvas canvas) {
        mPaint.setTextSize(0.5f * mLongRulingSize);
        mPaint.setTextAlign(Paint.Align.CENTER);
        canvas.rotate(-90);
        for (int i = 0; i < 180; i++) {
            canvas.drawLine(0, -mOutCircleRadius, 0, -mOutCircleRadius + mLongRulingSize * (i % 5 == 0 ? 1f : 0.5f), mPaint);
            if (i % 10 == 0 && i != 0 && i != 180) {
                canvas.drawText(i + "", 0, -mOutCircleRadius + mLongRulingSize * 1.5f, mPaint);
                canvas.drawLine(0, -mOutCircleRadius + 1.8f * mLongRulingSize, 0, -mOutCircleRadius + 2.3f * mLongRulingSize, mPaint);
                mPaint.setTextSize(0.4f * mLongRulingSize);
                canvas.drawText((180 - i) + "", 0, -mOutCircleRadius + mLongRulingSize * 2.8f, mPaint);
                canvas.drawLine(0, -mOutCircleRadius + mLongRulingSize * 3.2f, 0, -mInnerRadius, mPaint);
            }
            canvas.rotate(1);
        }
    }

    private void drawProtractorFrame(Canvas canvas) {
        RectF rectFOfCircle = new RectF(-mOutCircleRadius, -mOutCircleRadius, mOutCircleRadius, mOutCircleRadius);
        RectF rectFOfBottomSpan = new RectF(-mOutCircleRadius, 0, mOutCircleRadius, mBottomSpanHeight);

        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(mPanelColor));
        canvas.drawArc(rectFOfCircle, -180, 180, false, mPaint);
        canvas.drawRect(rectFOfBottomSpan, mPaint);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        canvas.drawArc(rectFOfCircle, -180, 180, false, mPaint);
        canvas.drawRect(rectFOfBottomSpan, mPaint);
        canvas.drawLine(0, -mOutCircleRadius, 0, 0, mPaint);


        RectF rectFOfInner = new RectF(-mInnerRadius, -mInnerRadius, mInnerRadius, mInnerRadius);
        canvas.drawArc(rectFOfInner, -170, 160, false, mPaint);
    }
}
